#include"PreProc.h"
#include"Calc.h"

void quick_sort(Split *Array, int p, int r);
void exchage(Split *from, Split *to);

int NomiProc(Set *tSet, Set *aSet)
{
    int offset, len;
    ANode *aMeb;
    DMeb  *tMeb;
    aMeb = (ANode *)(*aSet->SetMeb);
    while(aMeb->AId != STL)
    {
        if(aMeb->AType[0] =='N')
        {
            offset = aMeb->Aoffset;
            len = aMeb->ALen;
            tMeb = (DMeb *)(*tSet->SetMeb);
            while(tMeb->ID != STL)
            {
            }
        }
        aMeb = aMeb->NextMeb;
    }
    return 0;    
}

double OrdiProc(Set *tSet, ANode *aMeb)
{
    DMeb  *tMeb;
    char cVal[128];
    double *dVal, splitV;
    int i = 0, j;
    Split *tsp;
    int offset, len;
    int **s;
    int class, maxC = 0;
    double *G, minG = 1.0;

    s = (int **)malloc(sizeof(int *)*MAX_CHD);
    memset(s, 0, MAX_CHD*sizeof(int *));
    for(i=0;i<MAX_CLASS;i++)
    {
        s[i]=(int *)malloc(sizeof(int)*MAX_CLASS);
        memset(s[i], 0, MAX_CLASS*sizeof(int));
    }
    dVal = (double *)malloc((tSet->SetCnt)*sizeof(double));
    G = (double *)malloc((tSet->SetCnt)*sizeof(double));
    tsp = (Split *)malloc((tSet->SetCnt)*sizeof(Split));
    memset(cVal, 0, sizeof(cVal));
    memset(tsp, 0, (tSet->SetCnt)*sizeof(Split));
    memset(G, 0, (tSet->SetCnt)*sizeof(double));
    {
        if(aMeb->AType[0] =='O')
        {
            i = 0;
            offset = aMeb->Aoffset;
            len = aMeb->ALen;
            tMeb = (DMeb *)(*tSet->SetMeb);
            while(tMeb->ID != STL)
            {
                memcpy(cVal, tMeb->value + offset, len);
                dVal[i] = atof(cVal);
                tsp[i].dVal =  atof(cVal);
                tsp[i].class = tMeb->class;
                class = tMeb->class;
                maxC = (class>maxC)?class:maxC;
                tMeb = tMeb->NextMeb;     
                i ++;
            }
            quick_sort(tsp, 0, tSet->SetCnt - 1);
            i = 0;    
            while(i<tSet->SetCnt)
            {
                while(tsp[i].dVal==tsp[i + 1].dVal)
                {
                    if(i >=(tSet->SetCnt -1))
                        break;
                    i ++;
                }
                memset(s[0], 0, MAX_CLASS*sizeof(int));
                memset(s[1], 0, MAX_CLASS*sizeof(int));
                for(j=0;j<=i;j++)
                    s[0][tsp[j].class]++;
                for(j=i+1;j<tSet->SetCnt;j++)
                    s[1][tsp[j].class]++;
                G[i] = Entropy(s, maxC, 1);
                printf("Gini =[%f] [%d]\n", G[i], i);
                if(G[i]<minG)
                {
                    minG = G[i];
                    splitV = tsp[i].dVal;
                }
                i ++;
            }
            printf("splitV = [%f]\n", splitV);
        }
    }
    aMeb->Spv = splitV;    
    return splitV;
}

void quick_sort(Split *Array, int p, int r)
{
    int q;
    if(p<r) {
        q=Partion(Array,  p,  r);
        quick_sort(Array, p, q - 1);
        quick_sort(Array, q+1, r);
    }
    return;
}

int Partion(Split *Array, int p, int r)
{
    int i, j;
    Split *x;
    x=Array + r;
    i=p - 1;
    for (j=p;j<r;j++){
        if(Array[j].dVal<=x->dVal){
            i++;
            exchage(&Array[i], &Array[j]);
        }
    }
    exchage(&Array[i+1], &Array[r]);
    return i;
}

void exchage(Split *from, Split *to)
{
    Split i;

    memset(&i, 0, sizeof(Split)); 
    memcpy(&i, from, sizeof(Split));
    memcpy(from, to, sizeof(Split));
    memcpy(to, &i, sizeof(Split));
    return;
}
